using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input.Touch;
using ebookreader.Resources;

namespace ebookreader
{
    public class AlertPopup : PopUp
    {
        #region Fields
        string message, message2;
        Action onConfirm;
        #endregion

        public AlertPopup(Game game, String message, Action onConfirm)
            : base(game)
        {
            this.message = message;
            this.onConfirm = onConfirm;
        }
        State currentState;
        public override void Initialize()
        {
            //input_manager = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
            sprite_batch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            input_manager.OnTap += new InputManager.Tap(OnTap);
            currentState = Game.Services.GetService(typeof(State)) as State;
            input_manager.onBackButtonPressed += new InputManager.Back(() => { this.Dispose(); });
            DrawOrder = 6;
            base.Initialize();
        }


        

        public override void Draw(GameTime gameTime)
        {
            sprite_batch.Begin();
            sprite_batch.Draw(popup_back, GraphicsDevice.Viewport.Bounds, Color.White);
            sprite_batch.Draw(popup_read_body, new Vector2(225, 107), Color.White);
                       

            for (int i = 0; i < labels.Count; i++)
            {
                sprite_batch.DrawString(font_title, labels[i], positions[i], Color.Black);
            }

            sprite_batch.End();
            base.Draw(gameTime);
        }

        Texture2D popup_read_body, popup_back, button_ring, back, key;
        SpriteFont font, font_title;
        //VirtualKeyboard vk;
        protected override void LoadContent()
        {
            popup_read_body = Game.Content.Load<Texture2D>("Definitivo/popup_body");
            popup_back = Game.Content.Load<Texture2D>("todo/idle");
            button_ring = Game.Content.Load<Texture2D>("Definitivo/button_ring");
            back = Game.Content.Load<Texture2D>("Definitivo/black_back");
            font = font_title =Game.Content.Load<SpriteFont>("Fonts/menu_piccolo");


            splitLabel(message, font_title, new Rectangle(300, 160, 220, 95));
            //var mess = new LabelButton(sprite_batch, font, new Vector2(400-font.MeasureString(message).X/2, 110), Color.White, message);

            //entries.Add(mess);

            var cont = new BoxedLabelButton(sprite_batch, button_ring, new Vector2(303, 300), font, Lang.MM_Continue, Color.Black, true, currentState.options);
            cont.OnPress += new Press((me) =>
            {
                onConfirm();
                this.Dispose();
            });
            entries.Add(cont);


            var back_button = new TextureButton(sprite_batch, back, new Vector2(245, 126), currentState.options);
            back_button.OnPress += new Press(
                (me) => { this.Dispose(); }
                );
            entries.Add(back_button);

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            //if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Back))
            //    ScreenManager.SelectMainMenu(Game);
            //if (GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.Back))
            //    this.Dispose();
            base.Update(gameTime);
        }

        List<Vector2> positions = new List<Vector2>(), shadow_positions = new List<Vector2>();
        List<string> labels = new List<string>();

        void splitLabel(string label, SpriteFont font, Rectangle _box)
        {
            var prev_label_count = labels.Count;
            var capo = label.IndexOf('\n');
            var resto="";
            if (capo > 0)
            {
                resto = label.Substring(capo+1, label.Length-capo-1);
                label = label.Substring(0, capo);
            }
            
            var ls = label.Split(new char[] { ' '});
            var c_l = "";
            for (int i = 0; i < ls.Length; i++)
            {
                if (labels.Count > 2)
                {
                    labels[2] += "...";
                    break;
                };
                var l = " " + ls[i];
                while (font.MeasureString(l).X >= _box.Width)
                {
                    l = l.Substring(0, l.Length / 2) + "..";
                }
                if (font.MeasureString(c_l + l).X < _box.Width)
                    c_l += l;
                else
                {
                    labels.Add(c_l);
                    c_l = "";
                    i--;
                }
            }
            if (!c_l.Equals("")) labels.Add(c_l);


            if (!resto.Equals(""))
            splitLabel(resto, font, _box);

            for (int i = 0; i < labels.Count; i++)
            {
                var l = labels[i];
                var pos = new Vector2(_box.X,
                    _box.Y + _box.Height / 2 + ((i - labels.Count / 2) * (font.MeasureString("I").Y)) - font.MeasureString("I").Y / 2);
                positions.Add(pos);
                shadow_positions.Add(pos + Vector2.One * 2.0f);

            }

        }
    }
}
